﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;
using GSQ.Rpt_s;

namespace UIapp.Busquedas
{
    public partial class frmCotizaciones : Plantillas.frmFindPlantilla
    {
        public frmCotizaciones(decimal idCliente)
        {
            InitializeComponent();
            this.entityCollection = new List<COTIZACIONES>();
            this.anularFacturaToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "NullReport.png");
            this.reImprimirToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "printer.png");
            this.verEnPantallaToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "printDisplay.png");
            this.IdCliente = idCliente;
            this.backgroundWorker.RunWorkerAsync();
            Comun.ShowLoadingFRM();
        }
        #region Variables
        decimal IdCliente;
        List<COTIZACIONES> entityCollection;
        #endregion

        #region Metodos auxiliares
        public void Listar(decimal value)
        {
            this.entityCollection.Clear();
            this.entityCollection = new CotizacionesBL().GetByIdCliente(value).Where(a => a.ACTIVO).ToList();
        }
        #endregion

        #region Tratamiento de eventos

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Listar(this.IdCliente);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else
            {
                this.bindingSource.DataSource = this.entityCollection.ToList();
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.entityCollection.Count == 0)
            {
                e.Cancel = true;
                return;
            }
            if (((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ACTIVO)
                this.anularFacturaToolStripMenuItem.Enabled = true;
            else
                this.anularFacturaToolStripMenuItem.Enabled = false;
        }

        private void reImprimirToolStripMenuItem_Click(object sender, EventArgs e)
        {

            new Reportes().PrintCotizacion(((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ID, this.chkShowCodReference.Checked);

        }

        private void verEnPantallaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Plantillas.frmReportPreview frm = new Plantillas.frmReportPreview();
            frm.crystalReportViewer1.ReportSource = new Reportes().GetCotizacion(((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ID,this.chkShowCodReference.Checked);
            // frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (this.entityCollection.Count > 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
        #endregion
    }
}
